package org.stvd.repository.common.impl;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.stvd.entities.common.AuditDetail;
import org.stvd.repository.base.impl.BaseDaoImpl;
import org.stvd.repository.common.AuditDetailDao;

/**
 * 审核记录详情Repository实现类
 * @author houzx
 */
@Repository("AuditDetailDao")
public class AuditDetailDaoImpl extends BaseDaoImpl<AuditDetail> implements AuditDetailDao{

    @Override
    @Transactional
    public void deleteAuditDetailBySource(String sourceTable, String sourceId) {
        String sql = "DELETE FROM audit_detail WHERE flow_id IN ("
            + " SELECT id FROM audit_flow WHERE source_table = :sourceTable "
            + " AND source_id = :sourceId)";
        em.createNativeQuery(sql)
            .setParameter("sourceTable", sourceTable)
            .setParameter("sourceId", sourceId)
            .executeUpdate();
        em.flush();
    }

    @Override
    @Transactional
    public void deleteAuditDetailByFlowId(String flowId) {
        String sql = "DELETE FROM audit_detail WHERE flow_id = :flowId)";
        em.createNativeQuery(sql).setParameter("flowId", flowId).executeUpdate();
        em.flush();
    }

}
